/*
 * Entry.java
 *
 * Created on 7. Dezember 2006, 23:07
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.lesson;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * A key (as displayed int the HUD) and a value (as shown on a bot) pair.
 * The value should be normaly just 1 character for KanjiBots. A key
 * should be unique in one data file, the values don't have to.
 *
 * @author Pirx
 */
public class Entry {
    
    private String key;
    private LinkedList<String> values;
    private boolean ordered = true;
    private boolean dirty = false;
    
    public Entry() {
       values = new LinkedList<String>();
    }
    
    public Entry(String key, String[] values, boolean ordered) {
        this.key = key;
        this.values = new LinkedList<String>(Arrays.asList(values));
        this.ordered = ordered;
    }
    
    public String getKey() {
        return key;
    }
    
    public void addValue(String value) {
        values.add(value);
    }
    
    public List<String> getValues() {
        return values;
    }
    
    public boolean equals(Object o) {
        return getKey().equals(((Entry)o).getKey());
    }
    
    public String toString() {
        return getKey() + " -> " + values;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public boolean isOrdered() {
        return ordered;
    }

    public void setOrdered(boolean ordered) {
        this.ordered = ordered;
    }
    
    public boolean isSolved() {
        return values.isEmpty();
    }
    
    public boolean accept(String value) {
        if (isSolved()) {
            return false;
        }
        
        if (ordered) {
           if (values.getFirst().equals(value)) {
               values.removeFirst();
               dirty = true;
               return true;
           } else {
               return false;
           }
        } else {
            if (values.remove(value)) {
                dirty = true;
                return true;
            } else {
                return false;
            }
        }
    }
    
    public boolean isDirty() {
        return dirty;
    }
}